//
//  SLInformationViewController.m
//  StarLight
//
//  Created by 吴涛 on 16/5/9.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLInformationViewController.h"
#import "SLInformationViewModel.h"
#import "CDPImageCollectionView.h"
#import "BKJH5ViewController.h"
#import <MJRefresh.h>
#import "SLInformationCollectionViewCell.h"


@interface SLInformationViewController ()<UITableViewDelegate,CDPImageCollectionViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) SLInformationViewModel *infoViewModel;

@property (nonatomic, strong) CDPImageCollectionView *bannerView;

@property (nonatomic, strong) UITableView *newsListTableView;

@property (nonatomic ,strong) UICollectionView *headCollectionView;

@end

@implementation SLInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self.view addSubview:self.bannerView];
    [self.view addSubview:self.newsListTableView];
    [self.view addSubview:self.headCollectionView];
    [self addConstrains];
    [self addObservers];
    [self addLoadMoreAndRefresh];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [self.naviHeader setNaviTitleWithString:@"资讯服务"];
  
  self.view.backgroundColor = [UIColor defeutBackgroudColor];
}


- (void)addConstrains{
  
    [self.headCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(64);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(160);
        make.centerX.equalTo(self.view);
    }];
    
    [self.newsListTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headCollectionView.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).offset(-20);
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
    }];
}

- (void)addObservers{
  @weakify(self)
  
  [[RACObserve(self.infoViewModel,newsList)
    filter:^BOOL(NSMutableArray *value) {
    return value != nil;
  }]
   subscribeNext:^(NSMutableArray *list) {
     @strongify(self)
     [self.newsListTableView reloadData];
     [self setUpStauts];
  }];
  
  [[RACObserve(self.infoViewModel, bannerList) filter:^BOOL(NSMutableArray *value) {
    return value != nil;
  }] subscribeNext:^(NSMutableArray *list) {
    @strongify(self)
      [self.newsListTableView reloadData];
  }];
    
    [[RACObserve(self.infoViewModel, topnewsList) filter:^BOOL(NSMutableArray *value) {
        return value != nil;
    }] subscribeNext:^(NSMutableArray *list) {
        @strongify(self)
        [self.headCollectionView reloadData];
    }];
    
    
}

#pragma mark - refresh

- (void)addLoadMoreAndRefresh{
  @weakify(self)
  self.newsListTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    @strongify(self)
    [self.infoViewModel first];
    [self.newsListTableView.footer resetNoMoreData];
  }];
  
  self.newsListTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
    @strongify(self)
    [self.infoViewModel next];
  }];
  
}


- (void)setUpStauts{
  [self.newsListTableView reloadData];
  if (self.infoViewModel.newsList.count % 10 > 0) {
    [self.newsListTableView.footer endRefreshingWithNoMoreData];
  }else{
    [self.newsListTableView.footer endRefreshing];
  }
  
  [self.newsListTableView.header endRefreshing];
  
  [SVProgressHUD dismiss];
}



- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  
  if (!self.reach.isReachable) {
    return [[NSAttributedString alloc] initWithString:@"无网络连接, 请连接网络后刷新" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  }
  
  return [[NSAttributedString alloc] initWithString:@"暂无资讯!" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(SLInformationCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.infoViewModel.topnewsList.count != indexPath.row) {
        SLNewTopnews *model = self.infoViewModel.topnewsList[indexPath.row];
        [cell cellForData:model];
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SLInformationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SLInformationCollectionViewCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.infoViewModel.topnewsList.count;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SLNewTopnews *model = self.infoViewModel.topnewsList[indexPath.row];
    BKJH5ViewController *url = [[BKJH5ViewController alloc] init];
    url.h5Url = model.conturl;
    url.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:url animated:YES];
    
}


#pragma mark - CDPImageCollectionViewDelegate

- (void)didSelectImageWithNumber:(NSInteger)number{
  SLNewsBannerModel *model = self.infoViewModel.bannerList[number];
  BKJH5ViewController *h5VC = [BKJH5ViewController new];
  h5VC.h5Url = model.conturl;
  h5VC.hidesBottomBarWhenPushed = YES;
  [self.navigationController pushViewController:h5VC animated:YES];
}

#pragma mark - UITableViewDelegate


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  SLNewsModel *model = self.infoViewModel.newsList[indexPath.row];
  BKJH5ViewController *h5VC = [BKJH5ViewController new];
  h5VC.h5Url = model.conturl;
  h5VC.hidesBottomBarWhenPushed = YES;
  [self.navigationController pushViewController:h5VC animated:YES];
}

- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView{
  [self.newsListTableView.header beginRefreshing];
  [self.infoViewModel first];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - getter

- (UITableView *)newsListTableView{
  if (!_newsListTableView) {
    _newsListTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _newsListTableView.delegate = self;
    _newsListTableView.dataSource = self.infoViewModel;
    _newsListTableView.tableFooterView = [UIView new];
    _newsListTableView.layer.cornerRadius = 5.f;
    _newsListTableView.layer.masksToBounds = YES;
    [_newsListTableView registerClass:[SLInformationTableViewCell class] forCellReuseIdentifier:@"cell"];
    _newsListTableView.emptyDataSetSource = self;
    _newsListTableView.emptyDataSetDelegate = self;
    
  }
  return _newsListTableView;
}


- (SLInformationViewModel *)infoViewModel{
  if (!_infoViewModel) {
    _infoViewModel = [[SLInformationViewModel alloc] init];
  }
  return _infoViewModel;
}


- (UICollectionView *)headCollectionView
{
    if (!_headCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((SCREEN_WIDTH / 3.0) - 2, 78);
        layout.minimumInteritemSpacing = 1;
        layout.minimumLineSpacing = 2;
        _headCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [_headCollectionView registerClass:[SLInformationCollectionViewCell class] forCellWithReuseIdentifier:@"SLInformationCollectionViewCell"];
        _headCollectionView.backgroundColor =[UIColor defeutBackgroudColor];
        _headCollectionView.delegate = self;
        _headCollectionView.dataSource = self;
    }
    return _headCollectionView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
